from google.appengine.ext.db import djangoforms
from utils import str_is_not_blank
from model import *
import logging

class ProjectForm(djangoforms.ModelForm):
    class Meta:
        model = Project
        exclude = ['opened_by']
    def __init__(self, *args, **kwargs):
        super(ProjectForm, self).__init__(*args, **kwargs)
        self.fields['name'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all'})
        self.fields['name'].widget.attrs.update({'size':'50'})
        self.fields['description'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all'})
        self.fields['description'].widget.attrs.update({'rows':'5', 'cols': '50'})

class IterationForm(djangoforms.ModelForm):
    class Meta:
        model = Iteration
        exclude = ['_class', 'project', 'opened_by', 'status']
    def __init__(self, *args, **kwargs):
        super(IterationForm, self).__init__(*args, **kwargs)
        self.fields['name'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all'})
        self.fields['name'].widget.attrs.update({'size':'50'})
        self.fields['start_date'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all'})
        self.fields['end_date'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all'})
        self.fields['description'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all'})
        self.fields['description'].widget.attrs.update({'rows':'5', 'cols': '50'})

    def clean(self):
        start_date = self.cleaned_data.get('start_date')
        end_date = self.cleaned_data.get('end_date')
        if not start_date:
            self._errors["start_date"] = self.error_class(["This field is required."])
        if not end_date:
            self._errors["end_date"] = self.error_class(["This field is required."])
        logging.info(type(start_date))
        logging.info(end_date)
        if start_date and end_date:
            if end_date.date() < start_date.date():
                self._errors["end_date"] = self.error_class(["'End date' must be greater than 'Start date'."])

        return self.cleaned_data

class StoryForm(djangoforms.ModelForm):
    class Meta:
        model = Story
        exclude = ['_class', 'iteration', 'opened_by', 'assigned_to', 'status',
                   'story_container']
    def __init__(self, *args, **kwargs):
        super(StoryForm, self).__init__(*args, **kwargs)
        self.fields['name'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all', 'size': "50"})
        self.fields['estimated_time'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all',
                                                           'placeholder':"4.5, for example"})
        self.fields['category'].widget.attrs.update({'class':'select ui-widget-content ui-corner-all'})
        self.fields['priority'].widget.attrs.update({'class':'select ui-widget-content ui-corner-all'})
        self.fields['description'].widget.attrs.update({'class':'text ui-widget-content ui-corner-all'})
        self.fields['description'].widget.attrs.update({'rows':'5', 'cols':'50'})
    def clean(self):
        name = self.cleaned_data.get('name')
        if name:
            if not str_is_not_blank(name):
                self._errors["name"] = self.error_class(["This field is required, enter a string."])
        else:
            self._errors["name"] = self.error_class(["This field is required."])
        estimated_time = self.cleaned_data.get('estimated_time')
        if estimated_time:
            if estimated_time < 0:
                self._errors["estimated_time"] = self.error_class(["This field must be a positive number."])
        else:
            self._errors["estimated_time"] = self.error_class(["This field is required, enter a number."])
            
        return self.cleaned_data